# 平台名为NES的下一步应该寻求哪十个发行商合作

import pandas as pd
import numpy as np
from matplotlib import pyplot as plt


def topic_7(data: pd.DataFrame):

    # 设置绘图风格为ggplot
    plt.style.use("ggplot")

    # 设置中文编码和符号的正常显示
    plt.rcParams['font.family'] = 'sans-serif'
    plt.rcParams['font.sans-serif'] = ['SimHei', 'Arial Unicode MS']
    plt.rcParams["axes.unicode_minus"] = False

    # 删除重复项
    data.drop_duplicates(inplace=True)

    # 按发布商分组并计算销售额
    Platform_data = data.groupby(by=['Publisher'])[
        ['NA_Sales', 'EU_Sales', 'JP_Sales', 'Other_Sales', 'Global_Sales']].sum()

    # 按全球销售额排序
    Platform_data.sort_values(by=['Global_Sales'], inplace=True, ascending=False)

    # 选择前十个发布商
    Platform_data = Platform_data.head(10)

    # 提取销售数据
    sales_data = Platform_data[['NA_Sales', 'EU_Sales', 'JP_Sales', 'Other_Sales', 'Global_Sales']]

    # 创建一个图和坐标轴
    fig, ax = plt.subplots(figsize=(5, 3), dpi=200)

    # 设置柱子宽度
    w = 0.35

    # 绘制柱状图，堆叠各地区的销售额
    ax.bar(x=sales_data.index, height=sales_data['NA_Sales'], width=w, label='NA_Sales')
    ax.bar(x=sales_data.index, height=sales_data['EU_Sales'], bottom=sales_data['NA_Sales'], width=w, label='EU_Sales')
    ax.bar(x=sales_data.index, height=sales_data['JP_Sales'], bottom=sales_data['EU_Sales'], width=w, label='JP_Sales')
    ax.bar(x=sales_data.index, height=sales_data['Other_Sales'], bottom=sales_data['JP_Sales'], width=w,
           label='Other_Sales')

    # 设置图表标题和标签
    plt.title('各发布商销售额统计')
    plt.xlabel('发布商')
    plt.ylabel('销售额')

    # 显示图例
    plt.legend(loc='upper right')

    # 保存图表为图片文件
    plt.savefig('./static/img/topic_7.jpg')

    # 显示图表
    plt.show()
